SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE VIEW dbo.vw_ConceptosPorOrganizacionEstimulo
AS
SELECT DISTINCT A.Nombres, O.CUISE, O2.CUISE AS CuiseAFiltrar, O2.Nombre AS OrganizacionOrigen, O.Nombre AS OrganizacionDeCobro
FROM         dbo.tb_Liquidaciones L INNER JOIN
                      dbo.tb_RegistroDeLiquidaciones R ON L.idLiquidacion = R.Liquidacion INNER JOIN
                      dbo.tb_Organizaciones O2 ON O2.idOrganizaciones = L.Organizacion INNER JOIN
                      dbo.tb_ConceptosDelAgentePorOrganizacion C ON R.Agente = C.Agente INNER JOIN
                      dbo.tb_Organizaciones O ON O.idOrganizaciones = C.Organizacion INNER JOIN
                      dbo.tb_Agentes A ON A.idAgente = R.Agente
WHERE     (R.Concepto = 4) AND (C.Concepto = 33) AND EXISTS
                          (SELECT     *
                            FROM          tb_Liquidaciones L2 INNER JOIN
                                                   tb_RegistroDeLiquidaciones R2 ON L2.IdLiquidacion = R2.Liquidacion
                            WHERE      Concepto = 33 AND R2.Agente = IdAgente AND R2.MontoHaberes > 0 AND L2.Hasta = '31/07/2005') AND 
                      (L.Hasta = '31/07/2005')



GO
GRANT SELECT ON  [dbo].[vw_ConceptosPorOrganizacionEstimulo] TO [SoloVer]
GO
